<?php
namespace Cli\Controller;
use \Think\Controller;

class VipController extends \Think\Controller {

    public function run() {
        if (!IS_CLI) {
            echo 'error';
            return;
        }
        $orderInfoModel = D('OrderInfo');
        $vipInitModel = D('VipInit');
        $vipInfoModel = D('VipInfo');
        $vipUserModel = D('VipUser');
        $vipUserInfoModel = D('VipUserInfo');
        $adminHotelModel = D('AdminHotel');
        $starttime = date("Y-m-d H:i:s",strtotime("-1 day"));
        $endtime = date("Y-m-d H:i:s");
        $time = [
            $starttime,
            $endtime
        ];
        $option = [
            'status' => 3,
            'updatetime' => array('between',$time),
        ];
        $orderInfos = $orderInfoModel->where($option)->select();

        $checkinHotel = array_column($orderInfos, 'mobile');
        $i = 0;

        while($orderInfos[$i]){
            $vipUserInfo = $vipUserModel->getInfoByMobile($orderInfos[$i]['hotelentity_id'], $orderInfos[$i]['mobile'], $orderInfos[$i]['areacode']);
            //不是会员，处理下一个；
            if(empty($vipUserInfo)) {
                $i++;
                continue;
            }

            $vipInfo = $vipInfoModel->getInfoById($vipUserInfo['vipinfo_id']);

            //关闭自动升级，处理下一个；
            if($vipInfo['auto'] == '0') {
                $i++;
                continue;
            }

            $hotelCreator = $adminHotelModel->getHotelCreator($orderInfos[$i]['hotelentity_id']);

            $vipInitInfo = $vipInitModel->getInfoByAdmin($hotelCreator['adminlogin_id']);

            if($vipInitInfo['share'] == '1'){

                $adminHotelInfos = $adminHotelModel->getInfosByAdmin($hotelCreator['adminlogin_id']);
                //查询掌柜的酒店
                $hotelArray = array_column($adminHotelInfos, 'hotelentity_id');
                $hotelIds = implode(',',$hotelArray);

                $vipOrderInfo = $orderInfoModel->getCustomerByHotel($hotelIds, $orderInfos[$i]['mobile'], null, null, $orderInfos[$i]['areacode']);
                $nextLevel = $vipInfoModel->getNextLevelById($orderInfos[$i]['hotelentity_id'],$vipInfo['levelrule']);
                //满级
                if(empty($nextLevel)) {
                    $i++;
                    continue;
                }
                //判断升级方式
                if($vipInitInfo['level'] == '1'){
                    // $total = $vipOrderInfo[0]['deposit']+$vipOrderInfo[0]['receipt'];
                    $total = $vipOrderInfo[0]['receipt'];
                    //还没够金额
                    if($nextLevel['levelrule']>$total) {
                        $i++;
                        continue;
                    }
                } else {
                    //还没够次数
                    if($nextLevel['levelrule']>$vipOrderInfo[0]['checkin']) {
                        $i++;
                        continue;
                    }
                }

                
                $vipUserModel->startTrans();
                //修改会员等级需要循环找出会员等级Id再添加
                foreach ($adminHotelInfos as $adminHotelInfo) {
                    $vipLevel = $vipInfoModel->getInfoByHotelName($adminHotelInfo['hotelentity_id'],$nextLevel['name']);
                    $vipUser = $vipUserModel->getInfoByMobile($adminHotelInfo['hotelentity_id'], $orderInfos[$i]['mobile'], $orderInfos[$i]['areacode']);
                    $result = $vipUserModel->saveInfo($vipUser['id'], $vipLevel['id']);
                    if(empty($result)){
                        break;
                    }
                }
                if($result){
                    $vipUserModel->commit();
                }
                else{
                    $vipUserModel->rollback();
                }

                //共享需要释放掌柜下所有酒店相同的手机号码，以免重复升级
                foreach ($orderInfos as $key => $deleteOrder) {
                    if(in_array($deleteOrder['hotelentity_id'], $hotelArray) && $deleteOrder['mobile'] == $orderInfos[$i]['mobile'] && $deleteOrder['id'] != $orderInfos[$i]['id']){
                        unset($orderInfos[$key]);
                    }
                }

            }else{
                $vipOrderInfo = $orderInfoModel->getCustomerByHotel($orderInfos[$i]['hotelentity_id'], $orderInfos[$i]['mobile'], null, null, $orderInfos[$i]['areacode']);
                $nextLevel = $vipInfoModel->getNextLevelById($orderInfos[$i]['hotelentity_id'],$vipInfo['levelrule']);
                //满级
                if(empty($nextLevel)) {
                    $i++;
                    continue;
                }
                //判断升级方式
                if($vipInitInfo['level'] == '1'){
                    //$total = $vipOrderInfo[0]['deposit']+$vipOrderInfo[0]['receipt'];
                    $total = $vipOrderInfo[0]['receipt'];
                    //还没够金额
                    if($nextLevel['levelrule']>$total) {
                        $i++;
                        continue;
                    }
                } else {
                    //还没够次数
                    if($nextLevel['levelrule']>$vipOrderInfo[0]['checkin']) {
                        $i++;
                        continue;
                    }
                }
                $result = $vipUserModel->saveInfo($vipUserInfo['id'], $nextLevel['id']);
            }
            $i++;
        }


    }
}